package com.sxt.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.common.BussiException;
import com.sxt.common.CodeMsg;
import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.domain.BusCar;
import com.sxt.domain.BusRent;
import com.sxt.domain.BusReturn;
import com.sxt.mapper.BusCarDao;
import com.sxt.mapper.BusRentDao;
import com.sxt.mapper.BusReturnDao;
import com.sxt.query.BusReturnQuery;
import com.sxt.service.BusReturnService;
import com.sxt.shiro.ActiveUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 车辆归还记录业务实现
 */
@Service
public class BusReturnServiceImpl implements BusReturnService {
    @Autowired
    private BusRentDao busRentDao;

    @Autowired
    private BusCarDao busCarDao;

    @Autowired
    private BusReturnDao busReturnDao;

    /**
     * 新增还车记录
     *
     * @param busReturn
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(BusReturn busReturn) {
        // 查询出租记录
        BusRent busRent = busRentDao.selectByRentId(busReturn.getRentId());
        // 已经还车的情况
        if (busRent.getFlag().equals(Constant.CAR_RETURN_ED)) {
            return new Result(CodeMsg.RETURN_CAR_ERROR);
        }
        // 没有还车 则修改还车记录状态
        int rows = busRentDao.updateRentFlag(busReturn.getRentId(), busRent.getFlag(), Constant.CAR_RETURN_ED);
        if (rows != 1) {
            return new Result(CodeMsg.RETURN_FAILED_RENT_CHANGED_ERROR);
        }
        // 修改车辆状态 未出租
        BusCar busCar = busCarDao.selectOneByNum(busReturn.getNum());
        rows = busCarDao.updateRentState(busCar.getId(), Constant.CAR_RENT_NOT, busCar.getVersion());
        if (rows != 1) {
            throw new BussiException(CodeMsg.RETURN_FAILED_CAR_CHANG_ERROR.code, CodeMsg.RETURN_FAILED_CAR_CHANG_ERROR.msg);
        }
        // 计算出租总金额
        String beginTime = busRent.getBeginTime() + " 00:00:00";
        String returnTime = busReturn.getReturnTime() + " 23:59:59";
        int rentPrice = busRent.getRentPrice(); // 单日租金
        Date begin = DateUtil.parse(beginTime, "yyyy-MM-dd HH:mm:ss");
        Date end = DateUtil.parse(returnTime, "yyyy-MM-dd HH:mm:ss");
        int days = (int) DateUtil.betweenDay(begin, end, true) + 1;
        if (DateUtil.isSameDay(begin, end)) {
            days = 1;
        }
        rentPrice = days * rentPrice; // 计算总租金
        // 总金额
        int totalMoney = rentPrice + busReturn.getPayMoney();
        // 总租金
        busReturn.setRentPrice(rentPrice);
        busReturn.setTotalMoney(totalMoney);
        // 获取操作员ID
        Subject subject = SecurityUtils.getSubject();
        ActiveUser activUser = (ActiveUser) subject.getPrincipal();
        busReturn.setUserId(activUser.getSysUser().getId());
        // 新增还车记录
        busReturnDao.insert(busReturn);
        return new Result();
    }

    /**
     * 还车记录的分页查询
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(BusReturnQuery query) {
        Page<BusReturn> busReturnPage = PageHelper.startPage(query.getPage(), query.getLimit());
        busReturnDao.selectList(query);
        return new Result(busReturnPage.toPageInfo());
    }
}
